<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script src="{{ getAssets }}js/jquery.table2excel.min.js?v={{ assets_version }}"></script>
<script>
var tablesid;
var stocks;
var isLoading = false;
var columns_10 = [
    {"bSortable": false,render:function(data, type, full, meta) {
         return productGroupName(full.Products);
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.sph;
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.cyl;
    }}
];

var columns_20= [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(full.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.sph;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.cyl;
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Acceptance.lot_number;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return timetransDate(full.Acceptance.manufacture_date);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return timetransDate(full.Acceptance.expiry_date);
        }}
];

var columns_30 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(full.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.model;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.color;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.size;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.genre;
        }}
];

var columns_40 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(full.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.sph;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.add;
        }}
];

var columns_41 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(full.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.model;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.color;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.size;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.genre;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.film_color;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Snd.function_info;
        }}
];

var columns_50 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(full.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Acceptance.lot_number;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return timetransDate(full.Acceptance.manufacture_date);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return timetransDate(full.Acceptance.expiry_date);
        }}
];

var columns_60 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(full.Products);
        }}
];
var columns = {10 : columns_10,20 : columns_20,30 : columns_30,40 : columns_40,41 : columns_41,50 : columns_50,60 : columns_60} ;
var defcolumns = [
    {"bSortable": false,render:function(data, type, full, meta) {
            if(stocks.Auditing == 1) {
                return '<span id="PaperNumber-'+ full.Id +'"></span>';
            } else {
                return full.purchase_num;
            }
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
          if(stocks.Auditing == 1) {
             var html = '<span id="RealdiskNumber-'+ full.Id +'"></span>';
          } else {
            var html = '<input type="number" value="'+ full.purchase_num +'" oninput="change_losses_num(this)" min="0"  id="inv-'+ full.Id +'" name="reserve_real_num">';
          }
            html += '<input name="reserve_losses_num" type="hidden" value="0">';
            html += '<input name="reserve_num" type="hidden" value="'+ full.purchase_num +'">';
            html += '<input name="is_change" type="hidden" value="0">';
            html += '<input name="inventory_id" type="hidden" value="'+ full.Id +'">';
            if(full.Acceptance.lot_number) {
                html += '<input name="lot_number" type="hidden" value="'+ full.Acceptance.lot_number +'">';
                html += '<input name="expiry_date" type="hidden" value="'+ full.Acceptance.expiry_date +'">';
                html += '<input name="manufacture_date" type="hidden" value="'+ full.Acceptance.manufacture_date +'">';
            }
          return html;
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            if(stocks.Auditing == 1) {
                return '<span id="ProfitlossNumber-'+ full.Id +'">0</span>';
            } else {
                return '<div t_name="reserve_losses_num">0</div>';
            }
    }}
];

$(function(){

    $.post('', {}, function (res) {
        if(res.code == 0) {
            stocks = res.data.stocks;
            tablesid = $('#dataTables-'+stocks.Products.TypeId);
            var tableColumns = columns[stocks.Products.TypeId];
            var invdata = res.data.invdata;
            for(i=0;i<defcolumns.length;i++) {
                tableColumns.push(defcolumns[i]);
            }
            tablesid.DataTable({
                "sAjaxSource": ""
                , "bLengthChange": false
                , "columns": tableColumns
                , "serverSide": false
                , "bPaginate": false
                , "bFilter": false	//是否启动过滤、搜索功能
                , "bInfo": false //是否显示页脚信息，DataTables插件左下角显示记录数
                , "bProcessing": true //DataTables载入数据时，是否显示‘进度’提示
            });

            $.each(invdata, function(index, value){
                tablesid.DataTable().row.add(value).draw();
            });
            $.each(res.data.stocks_data, function(index, value){
                if(stocks.Auditing == 1) {
                    $('#PaperNumber-'+index).html(value.PaperNumber);
                    $('#ProfitlossNumber-'+index).html(value.ProfitlossNumber);
                    $('#RealdiskNumber-'+index).html(value.RealdiskNumber);
                } else{
                    $('input[id="inv-'+ index +'"]').val(value.RealdiskNumber);
                    change_losses_num($('input[id="inv-'+ index +'"]'));
                }
            });

            tablesid.show();
            if(stocks.Auditing == 1) {
                $('.submit-order').hide();
            } else {
                $('.submit-order').show();
            }
            isLoading = true;
        }
    }, 'json');

    $('.submit-order').click(function () {

        var data={};
        var _data  = {
            stocks_id:stocks.Id,
            datatable:[],
        };
        tablesid.find("tbody tr").each(function(){
            data={};
            data.reserve_num=Number($(this).find("input[name='reserve_num']").val());
            data.reserve_real_num=Number($(this).find("input[name='reserve_real_num']").val());
            data.reserve_losses_num=Number($(this).find("input[name='reserve_losses_num']").val());
            data.is_change=Number($(this).find("input[name='is_change']").val());
            data.inventory_id=Number($(this).find("input[name='inventory_id']").val());

            data.lot_number=$(this).find("input[name='lot_number']").val();
            data.expiry_date=Number($(this).find("input[name='expiry_date']").val());
            data.manufacture_date=Number($(this).find("input[name='manufacture_date']").val());

            _data.datatable.push(data);
        });
        if(_data.datatable.length<=0){
            layer.alert('未找到盘点商品');
            return;
        }
        layer.confirm("确认盘点信息无误？",function(index){
            layer.close(index);
            _data.datatable = JSON.stringify(_data.datatable);
            $.post('{{ urlfor "StocksController.SaveDetail" }}', _data, function (res) {
                if(res.code == 1) {
                    layer.alert(res.msg);return false;
                }
                location = '{{ urlfor "StocksController.ProfitLoss" }}/'+stocks.Id;
            });
        });
    });
});

function change_losses_num(obj){
    if(!$(obj).val()){
        $(obj).val(0);
    }else{
        $(obj).val(parseInt($(obj).val()));
    }
    var reserve_losses_num =  parseInt($(obj).val())-parseInt($(obj).parent().parent().find("[name='reserve_num']").val());
    if(reserve_losses_num == 0) {
        return;
    }
    $(obj).parent().parent().find("[name='reserve_losses_num']").val(reserve_losses_num);
    $(obj).parent().parent().find("[t_name='reserve_losses_num']").text(reserve_losses_num);
    $(obj).parent().parent().find("[name='is_change']").val(1);
}

$('#download').bind("click", function(){
    if(isLoading) {
        tablesid.table2excel({

            exclude: ".noExl",

            name: "导出库存盘点明细",

            filename: "库存盘点明细",

            fileext: ".xls",

            exclude_img: false,

            exclude_links: false,

            exclude_inputs: false
        });
    } else {
        $("#searchsubmit").click();
        layer.msg("还没加载结束请耐心等待一下！");return false;
    }
    return false;
});
</script>